package com.dragon.speech.controller;

import com.dragon.utils.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by jinlong.hao on 16/5/13.
 */
@RestController
public class TestController {

    public static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @RequestMapping("/test")
    public String test(@RequestParam(value = "name", defaultValue = "jinlong.hao") String name) {
        String todayStr = CommonUtil.formatDate(new Date(), "yyyy-MM-dd");
        logger.debug("test debug log, date : "+todayStr);
        return todayStr;
    }

    @RequestMapping("/test/obj")
    public TestObj testObj(@RequestParam(value = "name", defaultValue = "jinlong.hao") String name) {
        return new TestObj(1, name);
    }

    @RequestMapping("/test/map")
    public Map testMap(@RequestParam(value = "name", defaultValue = "jinlong.hao") String name) {
        Map<String, String> rMap = new HashMap<String, String>();
        rMap.put("name", name);
        rMap.put("mobile", "18611921111");
        return rMap;
    }

    @RequestMapping(path = "/test/file/upload", method = RequestMethod.POST)
    public Object testUploadFile(@RequestParam("file") MultipartFile file) {
        long fileSize = 0l;
        if (!file.isEmpty()) {
            fileSize = file.getSize();
            try {
                byte[] bytes = file.getBytes();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "File size: " + fileSize;
    }

    @RequestMapping("/test/redirect")
    public ModelAndView redirect() {
        return new ModelAndView("redirect:http://baidu.com");
    }

    @RequestMapping(path = "/test/image/get", method = RequestMethod.GET)
    public void getImage(HttpServletRequest request, HttpServletResponse response) {
        FileInputStream fis = null;
        response.setContentType("image/gif");
        try {
            OutputStream out = response.getOutputStream();
            File file = new File("/tmp/test1.png");
            fis = new FileInputStream(file);
            byte[] b = new byte[fis.available()];
            fis.read(b);
            out.write(b);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}

class TestObj {
    private int id;
    private String name;

    public TestObj(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
